//
//  ST_MyPhotoCollectionReusableView.swift
//  btvideo
//
//  Created by mac on 2020/1/7.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit

class ST_MyPhotoCollectionReusableView: UICollectionReusableView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        
        self.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(18)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalToSuperview()
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(upload_handle))
        avatar_image_view.isUserInteractionEnabled = true
        avatar_image_view.addGestureRecognizer(tap)
        
        self.addSubview(upload_avatar_btn)
        upload_avatar_btn.snp.makeConstraints { (make) in
            make.top.equalTo(avatar_image_view).offset(10)
            make.right.equalTo(avatar_image_view).offset(-10)
            make.width.equalTo(90)
            make.height.equalTo(30)
        }
        
        self.addSubview(tipsView)
        tipsView.snp.makeConstraints { (make) in
            make.width.equalTo(ST_MyAvatarMatchTipsView.size.width)
            make.height.equalTo(ST_MyAvatarMatchTipsView.size.height)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-10)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(refresh_tips_view), name: Notification.Name.face_status, object: nil)
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    MARK: - Properties
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel()
        
        let attrString = NSMutableAttributedString(string: "长按拖动调整[审核后]照片顺序")
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        control.attributedText = attrString
        
        control.textAlignment = .center
        
        return control
    }()
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 20
        return control
    }()
    
    
    fileprivate lazy var upload_avatar_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hex: 0xFEC207, transparency: 1)
        control.cornerRadius = 15
        control.setTitleForAllStates("更换头像")
        control.setTitleColorForAllStates(UIColor.white)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.addTarget(self, action: #selector(upload_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var tipsView:ST_MyAvatarMatchTipsView = {
        let control = ST_MyAvatarMatchTipsView()
        control.tipsText = "头像照片与你的真人识别不符，点击这里更换照片"
        control.isHidden = true
        return control
    }()
    
    
    
    //    MARK: - Bind Model
    var model = ST_MyPhotoCollectionCellModel(){
        didSet{
            
            if let img = model.localImage{
                avatar_image_view.image = img
            }else{
                
                avatar_image_view.pin_setImage(from: model.content.compress_image().url)
            }
            
            refresh_tips_view()
            
        }
    }
    
    var uploadClosure:(()->())?
}


extension ST_MyPhotoCollectionReusableView{
    
    
    @objc func upload_handle(){
        
//        if FACE_AVATAE_STATE == 4{
//            return
//        }
        
        uploadClosure?()
        
    }
    
    @objc func refresh_tips_view(){
        
        if [2,4].contains(FACE_AVATAE_STATE){
            tipsView.isHidden = false
        }else{
            tipsView.isHidden = true
        }
        tipsView.blue_color_style = FACE_AVATAE_STATE == 4
        
//        审核中不能换 头像
//        upload_avatar_btn.isHidden = FACE_AVATAE_STATE == 4
        
    }
    
}
